<?php

namespace App\Http\Controllers;

use App\lianxiTableModel;
use Illuminate\Http\Request;

class lianxiTableController extends Controller
{
    public function lianxitable()
    {
        $list=lianxiTableModel::get();
        return view('lianxitable',compact('list'));
    }

    public function add()
    {
        return view('lianxi');
    }

    public function insert(Request $request)
    {
        $data=$request->input();
        unset($data['-token']);
        $data['hobby']=implode("*",$data['hobby']);

        if($data['id']==0){
            $ok=lianxiTableModel::create($data);
        }else{
            $ok=lianxiTableModel::where("id",$data['id'])->first();
            $ok->update($data);
        }
        if($ok){
            return"信息添加成功！<br><a href='lianxitable'>返回首页</a>";
        }else{
            return "信息添加失败";
        }
    }


    public function del(Request $request)
    {
        $id=$request->input('id');
        $data=lianxiTableModel::where('id',$id)->first();
        $ok=$data->delete();
        if ($ok) {
            return "删除成功！<br><a href='lianxitable'>返回首页</a> <br><a href='add'>添加信息</a>";
        }
    }

    public function update(Request $request)
    {
        $id=$request->input('id');
        $data=lianxiTableModel::where('id',$id)->first();
        return view("lianxi",compact('data'));
    }
}
